package com.lvmama.rhino.job;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSONArray;
import com.lvmama.rhino.pojo.VisitorInfo;
import com.lvmama.rhino.service.VisitorService;
import com.lvmama.rhino.utils.DateUtil;
import com.lvmama.rhino.utils.HttpsUtil;

/**
 * @ClassName: AppVisitDataJob
 * @Description: ios 安卓每天定时拉取新房客数据
 * @author Administrator
 * @date 2017年5月20日 下午3:11:29
 */
public class AppVisitDataJob extends BaseQuartzJobBean {
	@Autowired
	private VisitorService visitorService;
	private Logger logger = Logger.getLogger(AppVisitDataJob.class);
	private static final String key = "Vkd0U1UxSkdWbFZYYldoYVZtMDRlRmt5TVhOTmF6VllVbXhTYVdGc1dtRldhazVPVFZkSmVsSnVUazlXYlhoM1ZUTndWazFYU2xWWmVrWlBWMFZhUjFSclVsTlNSazQyVm14d2JGWnRZM2hXUldoSFpXczFWVlp1YUZOV1JscG9Wa1JLTTAxV2JGZGFSV3hQWWtkNFNWVXdWVFZOVmxZMlZtNWFWVTFyV2tOYVJrNHdWa1UxV1ZGcVJtaFhSbG8yVmtSR2EyRXlSWGRQVmxKUFVqSjNlRlpZY0dGaU1sSllVbXR3YUUxc1duTlhWbVJ2VlVkR1ZXSklaR3RXVkd4RFdWWk9NR0ZHVFhwUmJYaHBWbFZ3ZVZONlJrNU5SMUpaVm01Q1dHSlVSWGhXYWtKTFlqQnplVkpzYUdwTlJHd3dWakJvVjA1V1NraGpSelZVWVRKb01GbHJhRXRTVjBwSVkwWndUMDFxVlhsV01uUnZUVzFTY21OSVdrNVNSbG96V2xaV2MxUnNXa2hqU0ZKcFVrWndXbGt3YUVOa01rcDBUVlJHVkdWVWJIcFpNRll3WWxkS1NHRkhhR3hpVkVaM1dURmFiMk15UmxaaVNHeHBZbFJHY0ZwSE1ERmtSMGw0Vlc1R1lVMUhlRFZaYTJSM1V6RndkR1JFUm1wWFNFSXhXV3hqTlZaWFNraGpla3BZVWpOb00xWXhaR0ZrTVc5NFlrY3hhMkpzY0V4Wk1qRXdUVlpzVmxWdVVtRk5TR2Q1V1ZST1YyRkdiSFJQV0d4cVlURktlbGx0ZUhkU1YwVjZWbXR3YW1KWVVYaFpha3BMWkVkR1ZtTkZiR2xpVkVZeVZtdGpOVTFzYkZoVGJrWmhUVWQzZVZsVVRsZFRiRXBJVFZoT2FVMXNiM2xhUjNSelRtMUtkVlpVUWsxTmFrWXlWa1ZrZDJNeVRuVlJWRTVyWW14d1MxcFhNSGhsVm14WFZXNVNhRmRGV2xwWk1HUnJXVlpXU0dSRVJsVlRSWEF5V1d4YWQyVlhTa2hhUm5CaFlsUkdlbGt5ZEZOa01rcEZXa1JLYUdKWFVrdFZNVkYzVUZFOVBRPT0=";
	
	@Override
	public void invoke(JobExecutionContext context) {
		Date beginDate = new Date();
		Calendar date = Calendar.getInstance();
		date.setTime(beginDate);
		date.set(Calendar.DATE, date.get(Calendar.DATE) - 1);
		String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(date
				.getTime());
		String sign = DigestUtils.md5Hex(yesterday+key);
		
		String jsons = HttpsUtil.requestGet(
				"http://m.lvmama.com/zhuanti/api/activity/getActivations?date="+yesterday+"&sign="+sign);
		logger.info("##############请求url:" + "http://m.lvmama.com/zhuanti/api/activity/getActivations?date="+yesterday+"&sign="+sign);
		logger.info("##########################################" + jsons);
		if (JSONObject.fromObject(jsons).get("code").toString().equals("1")) {
			String data = JSONObject.fromObject(jsons).get("data").toString();
			if (StringUtils.isNotEmpty(data)) {
				JSONArray json = JSONArray.parseArray(data);
				if (json.size() > 0) {
					List<VisitorInfo> visitorList = new ArrayList<VisitorInfo>();
					for (int i = 0; i < json.size(); i++) {
						VisitorInfo visitor = new VisitorInfo();
						com.alibaba.fastjson.JSONObject object = json
								.getJSONObject(i);
						String app_type = object.get("app_type").toString();
						String channel = object.get("channel").toString();
						int count = Integer.parseInt(object.get("count").toString());
						if(count>0){
							visitor.setAmount(count);
							visitor.setChannelCode(channel);
							visitor.setPlatformCode(app_type);
							visitor.setOperDate(DateUtil.parseDate(yesterday,
									"yyyy-MM-dd"));
							visitorList.add(visitor);
						}
					}
					visitorService.insertNewVisitorList(visitorList);
					logger.info("接口插入数据成功");
				}
			}
		} else {
			logger.info("接口返回失败,message:"
					+ JSONObject.fromObject(jsons).get("message").toString());
		}

	}

}
